<?php

class Admin_CategoriaController extends Zend_Controller_Action {

    private $_categoriaModelo;

    public function init() {

        $this->_helper->layout->setLayout('admin');

        $this->view->headLink()->appendStylesheet('/css/layout.css');
        $this->view->headScript()->appendFile('/js/jquery-1.3.2.min.js');
        $this->view->headScript()->appendFile('/js/easyTooltip.js');
        $this->view->headScript()->appendFile('/js/jquery-ui-1.7.2.custom.min.js');

        $this->view->headScript()->appendFile('/js/hoverIntent.js');
        $this->view->headScript()->appendFile('/js/superfish.js');

        $this->view->headScript()->appendFile('/js/jquery.alerts.js');
        $this->view->headLink()->appendStylesheet('/css/jquery.alerts.css');

        $this->view->headScript()->appendFile('/js/jquery.filestyle.mini.js');
        $this->view->headScript()->appendFile('/js/custom.js');

        $this->_categoriaModelo = new admin_Model_CategoriaMapper();
    }

    public function preDispatch() {
        if (admin_Model_Login::isLoggedIn()) {
            $this->view->loggedIn = true;
            $this->_administrador = admin_Model_Login::getIdentity();
        } else {
            $this->_redirect('/admin/login/');
        }
    }

    public function indexAction() {
        $this->view->headTitle('Listado de Categorias');
        $this->view->titulo = 'Listado de Categorias';
        $this->view->categorias = $this->_categoriaModelo->obtenerTodos();
        $this->view->administrador = $this->_administrador;
    }

    public function crearAction() {

        $form = new admin_Form_Categoria();
        $form->setAction('/admin/articulo/agregar/');
        $this->view->form = $form;
        $this->view->headTitle('Crear Categoría');
        $this->view->titulo == 'Crear Categoría';
        $this->view->administrador = $this->_administrador;
    }

    public function agregarAction() {

        $this->view->headTitle('Agregar Categoría');
        $this->view->titulo = 'Agregar Categoría';
        $this->view->administrador = $this->_administrador;

        $form = new admin_Form_Categoria();
        $form->setAction('/admin/categoria/agregar/'); // $this->_helper->url(array('agregar','usuario','admin'))

        if ($this->getRequest()->isPost()) {

            $params = $this->_request->getPost();

            if (!$form->isValid($params)) {

                $form->populate($params);
                $this->view->form = $form;
                return $this->render('crear');
            }

            $categoria = new admin_Model_Categoria();

            $categoria->setMetaKeywords($form->meta_keywords->getValue());
            $categoria->setMetaDescripcion($form->meta_descripcion->getValue());
            $categoria->setTitulo($form->titulo->getValue());
            $categoria->setNombre($form->nombre->getValue());
            $categoria->setH1($form->h1->getValue());
            $categoria->setDescripcion($form->descripcion->getValue());
            $categoria->setSlug($form->nombre->getValue());

            $this->_categoriaModelo->agregar($categoria);
            $this->_redirect('/admin/categoria/');
        }

        $this->view->form = $form;
        $this->render('crear');
    }

    public function editarAction() {

        $id = (int) $this->_request->getParam('id', 0);
        $form = new admin_Form_Categoria();
        $form->setAction('/admin/categoria/editar/id/' . $id);
        $this->view->headTitle('Editar Categoría');
        $this->view->titulo = 'Editar Categoría';
        $this->view->administrador = $this->_administrador;
        $data = $this->_categoriaModelo->obtenerPorId($id);

        if ($this->getRequest()->isPost()) {

            $params = $this->_request->getPost();

            if (!$form->isValid($params)) {

                $form->populate($params);
                $this->view->form = $form;
                return $this->render('crear');
            }

            $categoria = new admin_Model_Categoria();

            $categoria->setMetaKeywords($form->meta_keywords->getValue());
            $categoria->setMetaDescripcion($form->meta_descripcion->getValue());
            $categoria->setTitulo($form->titulo->getValue());
            $categoria->setNombre($form->nombre->getValue());
            $categoria->setH1($form->h1->getValue());
            $categoria->setDescripcion($form->descripcion->getValue());
            $categoria->setSlug($form->nombre->getValue());
            $categoria->setId($id);

            $this->_categoriaModelo->editar($categoria);
            $this->_redirect('/admin/categoria/editar/id/' . $id . '/');
        } else {

            if ($data === null) {
                $this->_redirect('/admin/categoria/');
            }

            $form->populate($data->toArray());
            $this->view->form = $form;
            $this->render('crear');
        }
    }

    public function eliminarAction() {

        $id = (int) $this->_request->getParam('id', 0);
        $this->_categoriaModelo->eliminar($id);
        $this->_redirect('/admin/categoria/');
    }

}